package com.android.base.ui.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;

import com.android.base.util.DisplayUtil;
import com.example.lib_base.R;
import com.example.lib_base.databinding.LibBaseDialogEditBinding;

/**
 * FileName: DialogEdit
 * Author: admin
 * Date: 2021/11/5 16:11
 * Description:
 */
public class DialogEdit extends Dialog {

    private LibBaseDialogEditBinding editBinding;
    private ActionLister actionLister;
    private Activity activity;
    private String hint;

    public DialogEdit(Activity a) {
        super(a, R.style.dialog_loading);
        this.activity = a;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        editBinding = LibBaseDialogEditBinding.inflate(activity.getLayoutInflater());
        setContentView(editBinding.getRoot());
        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = DisplayUtil.getScreenWidth(activity) / 5 * 4;
        getWindow().setAttributes(lp);
        setCancelable(false);
        setCanceledOnTouchOutside(false);

        if (!TextUtils.isEmpty(hint)) {;
            editBinding.edContent.setHint(hint);
        }
        editBinding.tvCancel.setText("取消");
        editBinding.tvEnter.setText("确定");
        editBinding.tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        editBinding.tvEnter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (actionLister != null) {
                    actionLister.onSuccess(DialogEdit.this, editBinding.edContent.getText().toString().trim());
                }
            }
        });
    }

    public DialogEdit setHint(String value) {
        this.hint = value;
        return this;
    }

    public DialogEdit setActionLister(ActionLister lister) {
        this.actionLister = lister;
        return this;
    }

    public interface ActionLister {
        void onSuccess(Dialog dialog, String value);
    }
}
